<!DOCTYPE html>
<html>

<head>
    <title>游记列表</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/travel.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css">
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script>
        $(function () {
            //游记页面的搜索功能
            $("#searchBtn").click(function () {
                var keyword = $("#search").val();
                if (keyword != null && keyword != "") {
                    location.href = "/searchPage.html?keyword=" + keyword + "&type=2";
                } else {
                    $(document).dialog({
                        titleText: '温馨提示',
                        content: '搜索内容不能为空!',
                    });
                }
            })
        })
    </script>
</head>

<body>


<div class="search-head">
    <div class="row nav-search">
        <div class="col-1">
            <a href="index.html">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
        <div class="col-9">
            <div class="input-group-sm search">
                <input id="search" class="form-control" placeholder="搜游记">
            </div>
        </div>
        <div class="col-1">
            <i class="fa fa-search" id="searchBtn"
               style="color:white;font-size:25px;position: absolute;top: 23%;left: 10%">
            </i>
        </div>
    </div>
</div>


<div class="container">

    <h6>每月冠军游记</h6>
    <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
        <div id="travelsCommends" class="carousel-inner">
            <div render-loop="list">
                <div class="carousel-item">
                    <div class="border commend">
                        <a data-url="travelContent.html?id=" render-key="list.travelId" render-fun="getHref">
                            <img render-src="list.commendCoverUrl" width="100%" height="200px">
                            <h1 render-html="list.commendTitle"> 骨灰级成都吃货地图</h1>
                            <small render-html="list.commendSubTitle"></small>
                        </a>
                    </div>
                </div>
            </div>
        </div>

        <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
            <span class="sr-only"></span>
        </a>
        <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
            <span class="sr-only"></span>
        </a>
    </div>
</div>

<div class="container">
    <h6>全部游记</h6>
    <div id="allTravels">
        <div render-loop="list">
            <div class="row detail">
                <div class="col">
                    <a render-key="list.id" render-fun="getHref"
                       data-url="/travelContent.html?id=" class="travel-id">
                        <h4 render-html="list.title"></h4>
                    </a>
                    <p>
                        <a render-key="list.author.id" render-fun="getUser" data-url="/userProfiles.html?id=">
                            <img class="rounded-circle head-img" render-src="list.author.headImgUrl">
                            <span render-html="list.author.nickName"></span> </a> 在
                        <span class="addr" render-html="list.place.name"></span>

                    </p>
                    <p>
                        <i class="fa fa-eye fa-fw"></i>
                        <span render-key="list.id" render-fun="getNum">0</span>
                        <a class="fa fa-commenting fa-fw" data-url="travelComment.html?id="
                           render-key="list.id" render-fun="getHref"></a>
                        <span  class="commentNum" render-html="list.tarvelCommentNum">0</span>
                    </p>
                </div>
                <div class="col bigimg">
                    <a render-key="list.id" render-fun="getHref" data-url="/travelContent.html?id=">
                        <img render-src="list.coverUrl">
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        var currentPage = 1;
        //总页数  用来判断是否是最后一页
        var pages;
        //[] 数组,一个容器 将每次拉倒底部查询的每页数据 数组都存放在里面
        var travelData = [];
        //变量表示 当前页是否已经在查询中 如果是 则不再查询,避免ajax的异步请求发送重复
        var loading = false;//查询状态
        //获取我的游记数据
        function query() {
            //如果当前为正在查询 则说明ajax已经发送 不需要重复发送
            if (loading) {
                return;
            }
            //每次在发送ajax前,将查询状态改为正在查询,
            loading = true;
            $.get("/travels", {currentPage: currentPage}, function (data) {
                //将查询出来的需要的数据的数组放到容器中
                $.merge(travelData, data.list);
                //render-loop="list"时 会找叫list的属性,所以需要将数据数组转为json对象的list属性
                var json = {list: travelData};
                //渲染数据
                $("#allTravels").renderValues(json, {
                    //参数item  render-key 和render-fun所在的标签
                    //value 就是render-key获取的值
                    'getHref': function (item, value) {
                        var url = $(item).data('url');
                        $(item).attr("href", url + value);
                        $(item).attr("data-id",value);

                    },
                    'getUser': function (item, value) {
                        var url = $(item).data('url');
                        $(item).attr("href", url + value);
                    },
                    getNum: function (item, value) {
                        var id = value;
                        //浏览量查看(渲染中使用同步请求)
                        //console.log(id);
                        $.ajax({
                            url: "/newsPages/scanNumber",
                            async: false,
                            data:{id:id},
                            success:function (data) {
                                //console.log($(item).find("span"));
                                $(item).html(data);

                            }
                        })

                    }
                });
                //当前页加1 下次将查询第二页数据
                currentPage++;
                //总页数
                pages = data.pages;
                //查询请求结果,则将查询状态改为 不在查询中
                loading = false;

            });
        }

        //默认的第一次查第一页数据
        query();
        //给window绑定滚动事件,当window滚动式,调用其中的函数
        $(window).scroll(function () {
            //判断是否滚动到底部(屏幕高度+HTML滚动超出顶部的高度>=HTML文档的高度)
            if ($(window).height() + $(document).scrollTop() + 1 >= $(document).height()) {
                //滚动到当前文档底部,并且是否还有页数可以显示 有则继续查询出来显示
                if (currentPage <= pages) {
                    query();
                    //滚动到底部时,如果此时还有页数 弹出加载中信息
                    $(document).dialog({
                        type: 'notice',
                        infoText: '加载中',
                        autoClose: 1500,
                        position: 'center'  // center: 居中; bottom: 底部
                    });
                } else {
                    //滚动到当前文档底部,如果已经是最后一页 则弹出已经到底
                    $(document).dialog({
                        type: 'notice',
                        infoText: '亲,已经到底了',
                        autoClose: 1500,
                        position: 'bottom'  // center: 居中; bottom: 底部
                    });
                }
            }
        });
        //所有游记推荐的
        $.get("/travels/commends", {type: 2, pageSize: 2}, function (data) {
            $("#travelsCommends").renderValues(data, {
                'getHref': function (item, value) {
                    var url = $(item).data("url");
                    $(item).attr("href", url + value);
                }
            });
            $(".carousel-item:first").addClass("active");
        });



    });
</script>

</body>

</html>